package com.lantu.transcoder;
import java.awt.GraphicsEnvironment;
import java.awt.geom.Rectangle2D;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.log4j.Logger;

import java.io.OutputStreamWriter;
public class SaveAsJPEG {
	private static final Logger logger = Logger.getLogger(SaveAsJPEG.class);
	public static void main(String[] args ){
		  String imgsrc = "http://localhost:8080/icon/1471448527661.jpg";
		//String svg="<svg id=\"SvgjsSvg1006\" width=\"100%\" height=\"100%\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:svgjs=\"http://svgjs.com/svgjs\"><defs id=\"SvgjsDefs1007\"></defs><image id=\"SvgjsImage1009\" xlink:href=\""+imgsrc+"\" width=\"255\" height=\"253\" transform=\"matrix(1,0,0,1,0,0)\" x=\"388\" y=\"56\"></image><rect id=\"SvgjsRect1008\" width=\"140\" height=\"198\" stroke=\"#000000\" fill=\"#ffffff\" stroke-width=\"1\" x=\"353\" y=\"84\" rx=\"0\" ry=\"0\" ></rect></svg>";
		  //String svg="<svg id=\"SvgjsSvg1006\" width=\"100%\" height=\"100%\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:svgjs=\"http://svgjs.com/svgjs\"><text id=\"SvgjsText1012\" font-family=\"Helvetica\" font-size=\"25\" text-anchor=\"middle\" x=\"455.375\" y=\"194\" data-text=\"中文测试\" data-bordercolor=\"#ff0033\" data-fill=\"#ff0033\" data-size=\"25\" data-family=\"Helvetica\" data-leading=\"1.5em\" fill=\"#ff0033\" stroke-opacity=\"1\" stroke=\"#ff0033\" data-anchor=\"middle\" style=\"fill: rgb(255, 0, 51);\" svgjs:data=\"{&quot;leading&quot;:&quot;1.5em&quot;}\"><tspan id=\"SvgjsTspan1025\" dy=\"37.5\" x=\"455.375\" svgjs:data=\"{&quot;newLined&quot;:true}\">中文测试</tspan></text></svg>";
		  //String svg="<svg id=\"SvgjsSvg1006\" width=\"100%\" height=\"100%\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:svgjs=\"http://svgjs.com/svgjs\"><defs id=\"SvgjsDefs1009\"><marker id=\"SvgjsMarker11526\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#00fe00\"><line id=\"SvgjsLine11527\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine11528\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker><marker id=\"SvgjsMarker12180\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#00fe00\"><line id=\"SvgjsLine12181\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine12182\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker><marker id=\"SvgjsMarker14133\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#00fe00\"><line id=\"SvgjsLine14134\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine14135\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker><marker id=\"SvgjsMarker14784\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#00fe00\"><line id=\"SvgjsLine14785\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine14786\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker><marker id=\"SvgjsMarker16632\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#00fe00\"><line id=\"SvgjsLine16633\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine16634\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker><marker id=\"SvgjsMarker17823\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#00fe00\"><line id=\"SvgjsLine17824\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine17825\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker><marker id=\"SvgjsMarker19887\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#00fe00\"><line id=\"SvgjsLine19888\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine19889\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker><marker id=\"SvgjsMarker20538\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#00fe00\"><line id=\"SvgjsLine20539\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine20540\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker><marker id=\"SvgjsMarker27503\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#00fe00\"><line id=\"SvgjsLine27504\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine27505\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker><marker id=\"SvgjsMarker28440\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#00fe00\"><line id=\"SvgjsLine28441\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine28442\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker></defs><image id=\"SvgjsImage1022\" xlink:href=\"http://www.lantudrawing.com/icon/1479362663769.png\" width=\"95\" height=\"143\" transform=\"matrix(1,0,0,1,0,0)\" x=\"253\" y=\"149\"></image><image id=\"SvgjsImage1100\" xlink:href=\"http://www.lantudrawing.com/icon/1479362594009.png\" width=\"91\" height=\"101\" transform=\"matrix(1,0,0,1,0,0)\" x=\"889\" y=\"185\"></image><image id=\"SvgjsImage1200\" xlink:href=\"http://www.lantudrawing.com/icon/1479362638459.png\" width=\"126\" height=\"97\" transform=\"matrix(1,0,0,1,0,0)\" x=\"598\" y=\"250\"></image><image id=\"SvgjsImage1575\" xlink:href=\"http://www.lantudrawing.com/icon/1479362855338.png\" width=\"104\" height=\"120\" transform=\"matrix(1,0,0,1,0,0)\" x=\"431\" y=\"146\"></image><line id=\"SvgjsLine2071\" x1=\"344\" y1=\"221\" x2=\"428\" y2=\"211\" stroke=\"#000000\" stroke-width=\"2\" data-width=\"2\" data-fill=\"#000000\" data-start_arrow=\"false\" data-end_arrow=\"true\" marker-end=\"url(#SvgjsMarker20538)\"></line><line id=\"SvgjsLine2130\" x1=\"510\" y1=\"146\" x2=\"588\" y2=\"102\" stroke=\"#000000\" stroke-width=\"2\" data-width=\"2\" data-fill=\"#000000\" data-start_arrow=\"false\" data-end_arrow=\"true\"  marker-end=\"url(#SvgjsMarker14784)\"></line><line id=\"SvgjsLine2303\" x1=\"515\" y1=\"260\" x2=\"602\" y2=\"313\" stroke=\"#000000\" stroke-width=\"2\" data-width=\"2\" data-fill=\"#000000\" data-start_arrow=\"false\" data-end_arrow=\"true\"  marker-end=\"url(#SvgjsMarker12180)\"></line><image id=\"SvgjsImage2549\" xlink:href=\"http://www.lantudrawing.com/icon/1479362699201.png\" width=\"102\" height=\"108\" transform=\"matrix(1,0,0,1,0,0)\" x=\"769\" y=\"29\"></image><line id=\"SvgjsLine3065\" x1=\"681\" y1=\"101\" x2=\"781\" y2=\"82\" stroke=\"#000000\" stroke-width=\"2\" data-width=\"2\" data-fill=\"#000000\" data-start_arrow=\"false\" data-end_arrow=\"true\"  marker-end=\"url(#SvgjsMarker17823)\"></line><text id=\"SvgjsText3823\" font-family=\"Helvetica\" font-size=\"25\" text-anchor=\"middle\" x=\"469.884033203125\" y=\"284\" data-text=\"web应用服务器\" data-bordercolor=\"#ff0033\" data-fill=\"#ff0033\" data-size=\"25\" data-family=\"Helvetica\" data-leading=\"1.5em\" fill=\"#ff0033\" stroke-opacity=\"1\" stroke=\"#ff0033\" data-anchor=\"middle\" style=\"fill: rgb(255, 0, 51);\" svgjs:data=\"{&quot;leading&quot;:&quot;1.5em&quot;}\"><tspan id=\"SvgjsTspan30661\" dy=\"37.5\" x=\"469.884033203125\" svgjs:data=\"{&quot;newLined&quot;:true}\">web应用服务器</tspan></text><text id=\"SvgjsText4183\" font-family=\"Helvetica\" font-size=\"25\" text-anchor=\"middle\" x=\"301.572021484375\" y=\"285\" data-text=\"app/网站\" data-bordercolor=\"#242424\" data-fill=\"#242424\" data-size=\"25\" data-family=\"Helvetica\" data-leading=\"1.5em\" fill=\"#242424\" stroke-opacity=\"1\" stroke=\"#242424\" data-anchor=\"middle\" style=\"fill: rgb(36, 36, 36);\" svgjs:data=\"{&quot;leading&quot;:&quot;1.5em&quot;}\"><tspan id=\"SvgjsTspan31299\" dy=\"37.5\" x=\"301.572021484375\" svgjs:data=\"{&quot;newLined&quot;:true}\">app/网站</tspan></text><text id=\"SvgjsText5168\" font-family=\"Helvetica\" font-size=\"20\" text-anchor=\"middle\" x=\"823.3125\" y=\"131\" data-text=\"应用服务器\" data-bordercolor=\"#6c6c6c\" data-fill=\"#909090\" data-size=\"20\" data-family=\"Helvetica\" data-leading=\"1.5em\" fill=\"#909090\" stroke-opacity=\"1\" stroke=\"#6c6c6c\" data-anchor=\"middle\" style=\"fill: rgb(144, 144, 144);\" svgjs:data=\"{&quot;leading&quot;:&quot;1.5em&quot;}\"><tspan id=\"SvgjsTspan6454\" dy=\"30\" x=\"823.3125\" svgjs:data=\"{&quot;newLined&quot;:true}\">应用服务器</tspan></text><text id=\"SvgjsText6908\" font-family=\"Helvetica\" font-size=\"20\" text-anchor=\"middle\" x=\"651.5625\" y=\"336\" data-text=\"图片服务器\" data-bordercolor=\"#242424\" data-fill=\"#6c6c6c\" data-size=\"20\" data-family=\"Helvetica\" data-leading=\"1.5em\" fill=\"#6c6c6c\" stroke-opacity=\"1\" stroke=\"#242424\" data-anchor=\"middle\" style=\"fill: rgb(108, 108, 108);\" svgjs:data=\"{&quot;leading&quot;:&quot;1.5em&quot;}\"><tspan id=\"SvgjsTspan7845\" dy=\"30\" x=\"651.5625\" svgjs:data=\"{&quot;newLined&quot;:true}\">图片服务器</tspan></text><image id=\"SvgjsImage21702\" xlink:href=\"http://www.lantudrawing.com/icon/1479362562859.png\" width=\"138\" height=\"108\" transform=\"matrix(1,0,0,1,0,0)\" x=\"568\" y=\"43\"></image><text id=\"SvgjsText23390\" font-family=\"Helvetica\" font-size=\"20\" text-anchor=\"middle\" x=\"639.524658203125\" y=\"144\" data-text=\"api网关服务器\" data-bordercolor=\"#ffcc00\" data-fill=\"#084b9c\" data-size=\"20\" data-family=\"Helvetica\" data-leading=\"1.5em\" fill=\"#084b9c\" stroke-opacity=\"1\" stroke=\"#ffcc00\" data-anchor=\"middle\" style=\"fill: rgb(8, 75, 156);\" svgjs:data=\"{&quot;leading&quot;:&quot;1.5em&quot;}\"><tspan id=\"SvgjsTspan24748\" dy=\"30\" x=\"639.524658203125\" svgjs:data=\"{&quot;newLined&quot;:true}\">api网关服务器</tspan></text><line id=\"SvgjsLine25381\" x1=\"864\" y1=\"113\" x2=\"926\" y2=\"184\" stroke=\"#000000\" stroke-width=\"2\" data-width=\"2\" data-fill=\"#000000\" data-start_arrow=\"false\" data-end_arrow=\"true\"  marker-end=\"url(#SvgjsMarker28440)\"></line></svg>";
		  String svg="<svg id=\"SvgjsSvg1041\"  width=\"100%\" height=\"100%\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:svgjs=\"http://svgjs.com/svgjs\"><defs id=\"SvgjsDefs1042\"></defs><rect id=\"SvgjsRect1043\" width=\"320\" height=\"200\" fill=\"#ffffff\" stroke=\"#000000\" stroke-width=\"1\" stroke-opacity=\"1\" x=\"423\" y=\"42.75\" data-bordercolor=\"#000000\" data-fill=\"#ffffff\" data-radius-x=\"100\" data-radius-y=\"100\" style=\"fill: rgb(39, 227, 52);\"></rect></svg>";
		System.out.println(svg);
		 double leftX = 423;//定义开始绘制的坐标
	        double leftY = 42.75;
	        float width2 = 319.99997f;//定义绘制宽度
	        float height2 = 200.0f;//定义绘制高度
		  try {
			SaveAsJPEG.svg2Jpeg(svg,"test.jpg",leftX,leftY,width2,height2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void svg2Jpeg(String svg1,String filename,double leftX,double leftY,float width,float height) {
		/*GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
				String[] fontFamilies = ge.getAvailableFontFamilyNames();
				System.out.println("支持的字体..");
				for (String s : fontFamilies) {
				    System.out.println(s);
				}
			*/	
        if(width<=0||width>1200){
        	width=1200;
        }
        if(height<=0||height>540){
        	height=540;
        }
        logger.info("width:"+width+",height:"+height+"leftX:"+leftX+",leftY:"+leftY);
		String svg=svg1.replace("fill=\"transparent\"", "fill-opacity=\"0\" ");
		logger.info(svg);
        // Create a JPEG transcoder
        JPEGTranscoder t = new JPEGTranscoder();
        t.addTranscodingHint(JPEGTranscoder.KEY_DEFAULT_FONT_FAMILY,"SimSun");
        t.addTranscodingHint(JPEGTranscoder.KEY_LANGUAGE,"cn");
        // Set the transcoding hints.
        t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
                   new Float(1));
        t.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, new Float(1200));//1200默认
        t.addTranscodingHint(JPEGTranscoder.KEY_HEIGHT, new Float(540));//540默认
       
       //t.addTranscodingHint(JPEGTranscoder.KEY_AOI,new Rectangle2D.Double(423, 42.75, 1200,400));//创建矩形对象());
        // Create the transcoder input.
        ByteArrayInputStream tInputStringStream;
		
		
       
        // Create the transcoder output.
        OutputStream ostream=null;
        String path="";
		try {
			tInputStringStream = new ByteArrayInputStream(svg.getBytes("UTF-8"));
			
	        //String svgURI = new File("test.svg").toURL().toString();
	        TranscoderInput input = new TranscoderInput(tInputStringStream);
	        String usrHome = System.getProperty("user.home");
	        path=usrHome+"/repo/paint/"+filename;
	        ostream = new FileOutputStream(path);
	        //System.out.println("###########"+usrHome);
			TranscoderOutput output = new TranscoderOutput(ostream);

	        // Save the image.
	        t.transcode(input, output);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TranscoderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			// Flush and close the stream.
	        try {
				ostream.flush();
				ostream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
		}
		
		// 第二次裁剪，去掉空白
		
		OperateImage o = new OperateImage((int)Math.floor(leftX) , (int)Math.floor(leftY),
				(int)Math.floor(width) + 2, (int)Math.floor(height) + 2);

		o.setSrcpath(path);
		o.setSubpath(path);
		try {
			o.cut();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }
}
